package cn.doctorpda.conference.ui.info;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.adapter.InfoAdapter;
import cn.doctorpda.conference.bean.doc.DataDocInfo;
import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.bean.doc.DocInfoItem;
import cn.doctorpda.conference.presenter.DocPresenter;
import cn.doctorpda.conference.ui.BaseFragment;
import cn.doctorpda.conference.ui.common.DetailActivity;
import cn.doctorpda.conference.utils.L;
import cn.doctorpda.conference.utils.T;
import cn.doctorpda.conference.view.callbackview.IDocPageView;
import cn.doctorpda.conference.view.recyclerview.XRecylcerView;
import okhttp3.Call;

/**
 * Created by huangjinlong on 2016/5/17.
 */
public class VideoFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, XRecylcerView.LoadingListener, IDocPageView {

    private SwipeRefreshLayout checkRefreshLayout;
    private XRecylcerView checkXRecylcerView;
    private InfoAdapter mAdapter;
    private DocPresenter docVideoPresenter;
    private int pageNumber = 1;

    private LinearLayout ll_no_data;
    private TextView tv_no_data;
    private ImageView imv_no_data;
    public static VideoFragment newInstance() {
        VideoFragment fragment = new VideoFragment();
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        mAdapter = new InfoAdapter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_info_common, container, false);
        initData();
        initView(view);
        return view;
    }

    @Override
    public String getPageName() {
        return "VideoFragment";
    }

    private void initData() {
        docVideoPresenter = new DocPresenter(this);
        docVideoPresenter.getDocData("1", "10", "video");


    }

    private void initView(View view) {
        checkRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.info_refresh);
        checkXRecylcerView = (XRecylcerView) view.findViewById(R.id.info_loadmore);
        checkXRecylcerView.setHasFixedSize(true);
        checkXRecylcerView.setLoadingListener(this);
        checkXRecylcerView.setLoadingMoreEnabled(true);
        checkRefreshLayout.setOnRefreshListener(this);
        checkXRecylcerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        checkXRecylcerView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
        mAdapter.setOnItemClickListener(new InfoAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, DocInfoItem data) {
                L.i("地址url" + data.getURL());
                DetailActivity.startActivity(getActivity(), data.getTITLE(),data.getID(), data.getTYPE(), data.getID());
            }
        });
        ll_no_data = (LinearLayout) view.findViewById(R.id.ll_no_data);
        tv_no_data = (TextView) view.findViewById(R.id.tv_no_data_show);
        imv_no_data = (ImageView) view.findViewById(R.id.imv_no_data_show);
    }

    @Override
    public void onLoadMore() {
        ++pageNumber;
        docVideoPresenter.getDocData(pageNumber + "", "10", "video");
    }

    @Override
    public void onRefresh() {
        pageNumber = 1;
        docVideoPresenter.getDocData("1", "10", "video");
        ll_no_data.setVisibility(View.INVISIBLE);
//        checkXRecylcerView.setVisibility(View.VISIBLE);
    }


    @Override
    public void setPageRefreshResult(Msg msg, DataDocInfo data) {
        // 设置当前刷新数，当下拉刷新的时候置为0，上拉加载时已经初始化
        checkXRecylcerView.setPreviousTotal(0);
        // 设置是否还有更多
        checkXRecylcerView.setIsnomore(false);
        checkRefreshLayout.setRefreshing(false);
        L.d("没数据");
        mAdapter.clear();
        if (data != null) {

            if (data.getPAGE().getList().isEmpty()||data.getPAGE().getTotalRow().equals("0")) {
                ll_no_data.setVisibility(View.VISIBLE);
                imv_no_data.setBackgroundResource(R.drawable.nodata);
                tv_no_data.setVisibility(View.VISIBLE);
                tv_no_data.setText("");
            }else {

                ll_no_data.setVisibility(View.INVISIBLE);
            }
//            ll_no_data.setVisibility(View.INVISIBLE);
            mAdapter.addAll(data.getPAGE().getList());
        }else {
            ll_no_data.setVisibility(View.VISIBLE);
            tv_no_data.setVisibility(View.INVISIBLE);
            imv_no_data.setBackgroundResource(R.drawable.shibai);
        }
    }

    @Override
    public void setPageLoadResult(Msg msg, DataDocInfo data) {
        checkXRecylcerView.loadMoreComplete();
        if(data!=null)
        mAdapter.addAll(data.getPAGE().getList());
    }

    @Override
    public void setPageError(Call call, Exception e) {
//        checkXRecylcerView.setVisibility(View.INVISIBLE);
        mAdapter.clear();
        checkXRecylcerView.loadMoreComplete();
        checkRefreshLayout.setRefreshing(false);
        ll_no_data.setVisibility(View.VISIBLE);
        tv_no_data.setVisibility(View.INVISIBLE);
        imv_no_data.setBackgroundResource(R.drawable.shibai);
    }
}
